package openbang.account.web.portal.widget;


import openbang.account.service.entity.UserAccount;
import openbang.account.service.exception.SecurityOBException;
import openbang.account.service.intf.AccountService;
import openbang.account.service.vo.BusinessEnum;
import openbang.account.service.vo.ExceptionEnum;
import openbang.account.service.vo.GradeEnum;
import openbang.account.service.vo.ImageParam;
import openbang.account.service.vo.PwdVerifyEnum;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 用户基本信息的显示和修改
 * 
 * 布局：
 * Grid 基本信息
 * 
 * 按钮栏  修改、上传头像、更换头像
 * 
 * 算法：
 * 进入该模块,自动从后台取出用户基本信息  UserAccount 对象
 * 
 * @author aibozeng
 *
 */
public class UserBaseInfoMaintainWidget extends Composite{
	//ROOT Widget
	VerticalPanel vPanelMenu = new VerticalPanel();
	
	//按钮栏
	HorizontalPanel btnPanel = new HorizontalPanel();
	Button updateBtn = new Button("修改信息");
	Button uploadBtn = new Button("上传头像");//更换头像
	Button updPhotoBtn = new Button("修改头像");
	Button refreshBtn = new Button("刷新");
	
	//基本信息
	Grid personGrid = new Grid(4,4);

	UserAccount userAccount=null;

	public UserBaseInfoMaintainWidget(){
		super();
		initWidget(vPanelMenu);
		vPanelMenu.add(personGrid);
		vPanelMenu.add(btnPanel);
		
		//按钮
		btnPanel.add(updateBtn);
		btnPanel.add(uploadBtn);
		btnPanel.add(updPhotoBtn);
		btnPanel.add(refreshBtn);
		updPhotoBtn.setEnabled(false);
		//由外部调用
		beginToReadData();
	}
	
	public void beginToReadData(){	
		AccountService.Util.getInstance().getUserAccount(new AsyncCallback<UserAccount>() {
			@Override
			public void onSuccess(UserAccount result) {				
				endToReadData(result);
			}
			@Override
			public void onFailure(Throwable caught) {
				if(caught instanceof SecurityOBException){
					SecurityOBException obe = (SecurityOBException)caught;
					Window.alert( ExceptionEnum.getDesc(obe.getCode()));
				}
				else{
					Window.alert(caught.getMessage());
				}				
			}
		});
	}

	public void endToReadData(UserAccount userAccountP){
		this.userAccount = userAccountP;
		if(userAccount==null){
			Window.alert("获取用户失败");
			return;
		}
		personGrid.setHTML(0, 0, "昵称:");
		personGrid.setHTML(0, 1, userAccount.getNiceName());
		personGrid.setHTML(0, 2, "Email:");
		personGrid.setHTML(0, 3, userAccount.getEmail());
		personGrid.setHTML(1, 0, "用户级别:");
		personGrid.setHTML(1, 1, GradeEnum.getDesc(userAccount.getGrade()));
		personGrid.setHTML(1, 2, "密码管理:");
		personGrid.setHTML(1, 3, PwdVerifyEnum.getDesc(userAccount.getVerifyType()));
		personGrid.setHTML(2, 0, "用户名称:");
		personGrid.setHTML(2, 1, userAccount.getUserName());
		personGrid.setHTML(2, 2, "注册日期:");
		personGrid.setHTML(2, 3, DateTimeFormat.getFormat("yyyy-MM-dd HH:mm").format(userAccount.getRegTime()));

		personGrid.setHTML(3, 0, "头像:");
		if(userAccount.getFacePhotoUrl()!=null){
			Image image = new Image();
			image.setTitle("头像");
			image.setUrlAndVisibleRect(userAccount.getFacePhotoUrl(), 0,0, ImageParam.FACEPHOTO_WIDTH, ImageParam.FACEPHOTO_HEIGTH);
			personGrid.setWidget(3, 1,image);
			//可以修改照片
			updPhotoBtn.setEnabled(true);
			updPhotoBtn.addClickHandler(new updPhotoBtnClickHandler());
			
		}
		else{
			personGrid.setHTML(3, 1,"No Photo");
		}
		uploadBtn.addClickHandler(new BtnClickHandler());
		updateBtn.addClickHandler(new BtnClickHandler());
	}
	
	public class BtnClickHandler implements ClickHandler{
		public void onClick(ClickEvent event) {
			String btnStr = ((Button)event.getSource()).getText();
			if(btnStr.equals(uploadBtn.getText())){
				Window.alert("图片服务暂未开通.Google APP Engine对此收费.");
				//Window.Location.replace("/upload.do?businessType="+BusinessEnum.UPLOAD_FACEPHOTO.getCode());				
			}
			else if( btnStr.equals(updateBtn.getText())){
				Window.alert("未实现");
				//Window.Location.replace("/upload.do?businessType="+BusinessEnum.UPLOAD_FACEPHOTO.getCode());				
			}
			else{				
			}
		}		
	}
	public class updPhotoBtnClickHandler implements ClickHandler{
		public void onClick(ClickEvent event) {
			Window.open("/displayPhoto.do?businessType="+BusinessEnum.DISPLAY_PHOTO.getCode()+"&outputType=html&mediaObjectId="+userAccount.getFacePhotoId(),"","");
		}		
	}
}
